home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 12 - 1996 / 12.10 Oct 96 / AdownsJavaObjects / Demo.java < prev    next >
Encoding:
Text File  |  1996-09-11  |  4.6 KB  |  197 lines  |  [TEXT/R*ch]

  1. /*
  2.  
  3. Listing 1: Demo.java
  4.  
  5. Demo.java
  6. Define and create a new instance of our applet.
  7.  
  8. */
  9.  
  10. // Import the generic Java classes.
  11. import java.applet.*;
  12. import java.awt.*;
  13. import java.io.*;
  14. import java.lang.*;
  15.  
  16. //    Our applet class definition.
  17. public    class Demo extends java.applet.Applet
  18. {
  19.     //    Instances of classes we need.
  20.     ToolPalette            theToolPalette;
  21.     public    Demo        myDemo;
  22.     BackdropCanvas        theBackdropCanvas;
  23.     NewButtonFrame        theNewButtonFrame;
  24.  
  25. //    Our first method.
  26.     public void init()
  27.     {
  28.         //    Variables local to this method, used only to setup our applet.
  29.         int            tempX = 320, tempY = 100;
  30.         int            tempWidth = 300, tempHeight = 300;
  31.         int            theColor;
  32.         Color        menuColor[] = {     Color.black, Color.green,
  33.                                                         Color.pink, Color.blue,
  34.                                                         Color.lightGray, Color.red,
  35.                                                         Color.cyan, Color.magenta,
  36.                                                         Color.white, Color.darkGray,
  37.                                                         Color.orange, Color.yellow,
  38.                                                         Color.gray };
  39.  
  40.         //    Setup our applet display area.
  41.         setBackground( Color.gray );
  42.         setForeground( Color.black );
  43.         setLayout( new BorderLayout() );
  44.  
  45.         //    We will want to refer to our applet later.
  46.         myDemo = this;
  47.  
  48.         //    Create an instance of our drawing canvas.
  49.         theBackdropCanvas = new BackdropCanvas();
  50.  
  51.         //    Now that our graphics canvas is ready, add it to the center of our applet.
  52.         add( "Center", theBackdropCanvas );
  53.  
  54.         //    Our tool palette.
  55.         theToolPalette = new ToolPalette( myDemo );
  56.  
  57.         //    A "dialog" to solicit input from the user.
  58.         theNewButtonFrame = new NewButtonFrame( myDemo );
  59.  
  60.         //    Retrieve some default color information from our tool palette.
  61.         theColor = theToolPalette.doGetColor( 0 );
  62.  
  63.         //    Call a method within our graphics canvas and tell it to set the background color.
  64.         theBackdropCanvas.doSetBackgroundColor( 
  65.                 menuColor[ theColor ] );
  66.  
  67.         //    Our applet should draw everything it knows about the first time through.
  68.         repaint();    
  69.     }
  70.  
  71.     //    Haven't made use of this yet.
  72.     public void start()
  73.     {
  74.     }
  75.  
  76.     //    Stop loading/running when requested.
  77.     public void stop()
  78.     {
  79.         myDemo.doDestroy();
  80.         System.exit( 0 );
  81.     }
  82.  
  83.     //    Graceful exit when requested.
  84.     public void doDestroy()
  85.     {
  86.         theToolPalette.hide();
  87.         theBackdropCanvas.hide();
  88.         theNewButtonFrame.hide();
  89.         System.exit( 0 );
  90.     }
  91.  
  92.     //    Create a panel/rectangle object upon request from the tool palette.
  93.     public boolean doAddRect()
  94.     {
  95.         boolean        doRepaint = false;
  96.         int                theColor;
  97.         String        theNewString = new String( "" );
  98.  
  99.         theColor = theToolPalette.doGetColor( 1 );
  100.         theBackdropCanvas.doChangeColor( 1, theColor );
  101.  
  102.         theColor = theToolPalette.doGetColor( 2 );
  103.         theBackdropCanvas.doChangeColor( 2, theColor );
  104.  
  105.         doRepaint = theBackdropCanvas.doAddDecafObject( 0,
  106.                 theNewString );
  107.  
  108.         if ( doRepaint )
  109.             theBackdropCanvas.repaint();
  110.  
  111.         return true;
  112.     }
  113.  
  114.     //    Setup our dialog that will request a button label from the user.
  115.     public boolean doCreateButtonFrame()
  116.     {
  117.         boolean        doRepaint = false;
  118.         boolean        dummy = false;
  119.  
  120.         dummy = theNewButtonFrame.doSetupDefaults();
  121.         theNewButtonFrame.show();
  122.  
  123.         if ( doRepaint )
  124.             repaint();
  125.  
  126.         return true;
  127.     }
  128.  
  129.     //    Add a "button" object upon request from the tool palette.
  130.     public boolean doAddButton( String theNewString )
  131.     {
  132.         boolean        doRepaint = false;
  133.         int                theColor;
  134.  
  135.         theColor = theToolPalette.doGetColor( 3 );
  136.         theBackdropCanvas.doChangeColor( 3, theColor );
  137.  
  138.         theColor = theToolPalette.doGetColor( 4 );
  139.         theBackdropCanvas.doChangeColor( 4, theColor );
  140.  
  141.         doRepaint = theBackdropCanvas.doAddDecafObject( 1,
  142.                 theNewString );
  143.  
  144.         if ( doRepaint )
  145.             theBackdropCanvas.repaint();
  146.  
  147.         return true;
  148.     }
  149.  
  150.     //    Change the color for one or more of our objects upon request from the tool palette.
  151.     public boolean doChangeColor( int theItem, int theColor )
  152.     {
  153.         boolean        doRepaint = false;
  154.  
  155.         doRepaint = theBackdropCanvas.doChangeColor( theItem,
  156.                 theColor );
  157.  
  158.         if ( doRepaint )
  159.             theBackdropCanvas.repaint();
  160.  
  161.         return true;
  162.     }
  163.  
  164.     //    Delete one selected item upon request from the tool palette.
  165.     public boolean doDeleteItem()
  166.     {
  167.         boolean        doRepaint = false;
  168.  
  169.         doRepaint = theBackdropCanvas.doDeleteDecafObject();
  170.  
  171.         if ( doRepaint )
  172.             theBackdropCanvas.repaint();
  173.  
  174.         return true;
  175.     }
  176.  
  177.     //    Change the background color for our drawing area on request from the tool palette.
  178.     public boolean doSetBackColor( int theColor )
  179.     {
  180.         Color        menuColor[] = {     Color.black, Color.green,
  181.                                                         Color.pink, Color.blue,
  182.                                                         Color.lightGray, Color.red,
  183.                                                         Color.cyan, Color.magenta,
  184.                                                         Color.white, Color.darkGray,
  185.                                                         Color.orange, Color.yellow,
  186.                                                         Color.gray };
  187.  
  188.         theBackdropCanvas.doSetBackgroundColor( menuColor[
  189.                 theColor ] );
  190.  
  191.         theBackdropCanvas.repaint();
  192.  
  193.         return true;
  194.     }
  195.  
  196.